# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:news_detail1_3.py
@Time:2024/12/9 17:56

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import csv
import time

# 假设的 CSV 文件路径
file_path = '../news/outputnews_2024.csv'
# 假设的输出文件路径
output_file_path = 'newsdetail/news_detail1_3.csv'

# 设置 Chrome 选项以在后台运行
chrome_options = Options()
chrome_options.add_argument("--headless")

# 初始化 WebDriver
# service = Service('d:/code_zgq/ganbu/chromedriver')  # 替换为你的 chromedriver 的路径
# driver = webdriver.Chrome(service=service, options=chrome_options)
driver_path = 'd:/code_zgq/ganbu/chromedriver.exe'
# driver_path = driver_DIR
service = Service(executable_path=driver_path)

# 创建 WebDriver 实例
# driver = webdriver.Chrome(service=service)
driver = webdriver.Chrome()
driver.implicitly_wait(5)
# driver.maximize_window()


# 用于跟踪每个日期遇到的第一个链接
first_link_of_day = {}

# 保存新闻详情的列表
news_details = []

# 定义一个函数来获取网页内容
def fetch_news_content(link):
    try:
        driver.get(link)
        content_area = driver.find_element(By.ID, 'content_area')
        return link, content_area.text if content_area else ''
    except Exception as e:
        print(f"处理链接 {link} 时出错: {e}")
        return link, ''

# 读取 CSV 文件并准备链接列表
links_to_fetch = []
with open(file_path, mode='r', encoding='utf-8') as file:
    reader = csv.DictReader(file)
    for row in reader:
        date, title, link = row['日期'], row['标题'], row['链接']
        if date not in first_link_of_day:
            first_link_of_day[date] = link
            continue
        links_to_fetch.append((date, title, link))

# 开始计时
start_time = time.time()

# 遍历链接并获取内容
for date, title, link in links_to_fetch:
    print(date)
    _, content_text = fetch_news_content(link)
    news_details.append([date, title, link, content_text])

# 结束计时
end_time = time.time()

# 计算并打印执行时间
execution_time = end_time - start_time
print(f"执行时间: {execution_time} 秒")

# 关闭 WebDriver
driver.quit()

# 写入文件
with open(output_file_path, mode='w', encoding='utf-8', newline='') as output_file:
    writer = csv.writer(output_file)
    writer.writerow(['日期', '标题', '链接', '内容'])
    writer.writerows(news_details)
